package com.app.newiptv.view;

import android.content.Context;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.Scroller;
import android.widget.TextView;
/**
 * 滚动的textview
 * 使用先将view进行显示
 * @author guotong
 *
 */
	public class ScrollTextView extends TextView {

    // scrolling feature
    private Scroller mSlr;

    // 一轮滚动的毫秒数
    private int mRndDuration =20000;

    // 暂停时的位置
    private int mXPaused = 0;

    // 是否被暂停
    private boolean mPaused = true;

    public ScrollTextView(Context context) {
        this(context, null);
        setSingleLine();
        setEllipsize(null);
        setVisibility(INVISIBLE);
    }
    public ScrollTextView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
        setSingleLine();
        setEllipsize(null);
        setVisibility(INVISIBLE);
    }
    public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setSingleLine();
        setEllipsize(null);
        setVisibility(INVISIBLE);
    }

    /**
     * 开始从原始位置滚动文本.
     */
    public void startScroll() {
        // begin from the very right side
        mXPaused = -1 * getWidth();
        // assume it's paused
        mPaused = true;
        resumeScroll();
    }

    /**
     * 从暂停点继续滚动
     */
    public void resumeScroll() {

        if (!mPaused)
            return;

        // Do not know why it would not scroll sometimes
        // if setHorizontallyScrolling is called in constructor.
        int scrollingLen = calculateScrollingLen();

        setHorizontallyScrolling(true);

        // 使用linearinterpolator稳态滚动
        mSlr = new Scroller(this.getContext(), new LinearInterpolator());
        setScroller(mSlr);
        int distance = scrollingLen - (getWidth() + mXPaused);
//        int duration = (new Double(mRndDuration * distance * 1.00000
//                / scrollingLen)).intValue();
        int duration = distance * 8;
        setVisibility(VISIBLE);
        mSlr.startScroll(mXPaused, 0, distance, 0, duration);
        invalidate();
        mPaused = false;
    }

    /**
     * 计算像素中文本的滚动长度
     *
     * 
     */
    private int calculateScrollingLen() {
        TextPaint tp = getPaint();
        Rect rect = new Rect();
        String strTxt = getText().toString();
        tp.getTextBounds(strTxt, 0, strTxt.length(), rect);
        int scrollingLen = rect.width() + getWidth();
        rect = null;
        return scrollingLen;
    }

    /**
     * 暂停滚动文本
     */
    public void pauseScroll() {
        if (null == mSlr)
            return;

        if (mPaused)
            return;

        mPaused = true;

        // abortAnimation sets the current X to be the final X,
        // and sets isFinished to be true
        // so current position shall be saved
        mXPaused = mSlr.getCurrX();

        mSlr.abortAnimation();
    }

    
     /*
     * 重写computescroll重启滚动完成时，
     * 文本滚动永远
     */
    @Override
    public void computeScroll() {
        super.computeScroll();

        if (null == mSlr) return;

        if (mSlr.isFinished() && (!mPaused)) {
            this.startScroll();
        }
    }
    
//    public int getRndDuration() {
//        return mRndDuration;
//    }
//
//    public void setRndDuration(int duration) {
//        this.mRndDuration = duration;
//    }
//
//    public boolean isPaused() {
//        return mPaused;
//    }
}
